﻿-- Author      : Kurapica

--[[
	Advance Frame for TabGroup
--]]
do
	local CreateFrame = IGAS.GUI.CreateFrame
	local _Widget = IGAS.GUI.Widget.TabGroup
	local twipe = table.wipe
	local tinsert = table.insert

	-- From
	local advF = CreateFrame("Frame", "Advance_TabGroup", IGAS.Studio.UIParent)

	advF.Height = 200
	advF.Width = 130
	advF.FrameStrata = "DIALOG"
	advF.Toplevel = true
	advF.Movable = true
	advF.MouseEnabled = true
	advF.Visible = false
	advF:SetBackdrop{
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 16, edgeSize = 16,
		insets = { left = 3, right = 3, top = 3, bottom = 3 }
	}
	advF:SetBackdropColor(0,0,0,1)
	advF:SetPoint("CENTER", UIParent, "CENTER")

	function advF:OnMouseDown()
		self:StartMoving()
	end

	function advF:OnMouseUp()
		self:StopMovingOrSizing()
	end

	-- ScrollForm
	local scF = CreateFrame("ScrollForm", "ScrollForm", advF)

	scF:SetPoint("TOPLEFT", advF, "TOPLEFT", 6, -16)
	scF:SetPoint("BOTTOMRIGHT", advF, "BOTTOMRIGHT", -6, 30)
	scF.ContainerHeight = 26

	local container = scF.Container

	local btnOK = CreateFrame("Button", "Okay", advF)
	btnOK.Text = "Okay"
	btnOK.Height = 24
	btnOK.Width = 60
	btnOK.Style = "NORMAL"
	btnOK:SetPoint("BOTTOMLEFT", advF, "BOTTOMLEFT", 4, 4)

	local btnCancel = CreateFrame("Button", "Cancel", advF)
	btnCancel.Text = "Cancel"
	btnCancel.Height = 24
	btnCancel.Width = 60
	btnCancel.Style = "NORMAL"
	btnCancel:SetPoint("LEFT", btnOK, "RIGHT", 0, 0)

	local namePop = CreateFrame("PopupDialog", "NamePop", advF)
	namePop.Message = "Please Input the TabPage's name"
	namePop.ShowInputBox = true
	namePop.ShowNoButton = false

	-- Scripts
	container.__TabNum = 0

	local btnAdd = CreateFrame("Button","AddBtn",container)
	btnAdd.Visible = true
	btnAdd.Width = 24
	btnAdd.Height = 24
	btnAdd:SetPoint("TOPLEFT", container, "TOPLEFT", 0, 0)

	btnAdd:SetNormalTexture("Interface\\Buttons\\UI-PlusButton-UP")
	btnAdd:SetPushedTexture("Interface\\Buttons\\UI-PlusButton-DOWN")
	btnAdd:SetHighlightTexture("Interface\\Buttons\\UI-PlusButton-Hilight")
	local t = btnAdd:GetHighlightTexture()
	t:SetBlendMode("ADD")

	local btn, id, delBtn, tab
	local _TabList = {}

	local function DelTabBtn(id)
		_TabList[container:GetChild("TabBtn"..id).Text] = false
				
		container:GetChild("DelBtn"..id):Dispose()
		container:GetChild("TabBtn"..id):Dispose()
		
		while id < container.__TabNum do
			id = id + 1
			btn = container:GetChild("TabBtn"..id)
			btn.ID = id - 1
			btn.Name = "TabBtn"..(id-1)
			btn:SetPoint("TOPLEFT", container, "TOPLEFT", 0, -24 * (id - 2))
			
			delBtn = container:GetChild("DelBtn"..id)
			delBtn.ID = id - 1
			delBtn.Name = "DelBtn"..(id-1)
		end
		
		container.__TabNum = container.__TabNum - 1
		btnAdd:SetPoint("TOPLEFT", container, "TOPLEFT", 0, -24 * (container.__TabNum))
		container.Height = 24 * (container.__TabNum) + 24    
	end

	local function btnDel_OnClick(self)
		-- This would not be used frequently,
		-- so I just dispose them
		DelTabBtn(self.ID)
	end

	local function AddTabBtn(name)
		if not name or type(name) ~= "string" or string.gsub(name, " ", "") == "" or _TabList[name] then
			return
		end
		
		_TabList[name] = true
		
		container.__TabNum = container.__TabNum + 1
		
		container.Height = 24 * (container.__TabNum) + 24
		btnAdd:SetPoint("TOPLEFT", container, "TOPLEFT", 0, -24 * (container.__TabNum))
		
		btn = CreateFrame("Button", "TabBtn"..container.__TabNum, container)
		btn.ID = container.__TabNum
		btn.Height = 24
		btn.Text = name
		btn.Width = 60
		btn.Style = "NORMAL"
		btn:SetPoint("TOPLEFT", container, "TOPLEFT", 0, -24 * (container.__TabNum - 1))
		
		delBtn = CreateFrame("Button","DelBtn"..container.__TabNum, container)
		delBtn.ID = container.__TabNum
		delBtn.Width = 24
		delBtn.Height = 24
		delBtn:SetPoint("LEFT", btn, "RIGHT")
		
		delBtn:SetNormalTexture("Interface\\Buttons\\UI-MinusButton-UP")
		delBtn:SetPushedTexture("Interface\\Buttons\\UI-MinusButton-DOWN")
		delBtn:SetHighlightTexture("Interface\\Buttons\\UI-PlusButton-Hilight")
		t = delBtn:GetHighlightTexture()
		t:SetBlendMode("ADD")
		delBtn.OnClick = btnDel_OnClick
				
		return btn
	end

	local function Clear()
		-- Clear
		twipe(_TabList)
		
		for i = container.__TabNum, 1, -1 do
			DelTabBtn(i)
		end
		
		advF.__Frame = nil
	end
	
	function btnAdd:OnClick()
		namePop:Show()
	end

	function namePop:OnOkay()
		AddTabBtn(self.Text)
	end

	function btnOK:OnClick()
		for i = 1, container.__TabNum do
			tab = advF.__Frame:GetTabByIndex(i)
			btn = container:GetChild("TabBtn"..id)
			
			if tab.Text ~= btn.Text then
				tab.Text = btn.Text
			end
		end
		
		-- Remove else
		for i = advF.__Frame.TabNum, container.__TabNum + 1, -1 do
			advF.__Frame:GetChild("TabBtn"..id):RemoveTabByIndex(i)
		end
		
		Clear()		
		advF:Hide()
	end
	
	function btnCancel:OnClick()
		Clear()
		advF:Hide()
	end
	
	-- start advance setting for the frame
	function advF:SetFrame(frame)
		-- Clear
		Clear()
		
		self.__Frame = frame
		
		-- Add TabPages
		for i = 1, frame.TabNum do
			tab = frame:GetTabByIndex(i)
			AddTabBtn(tab.Text)
		end
	end

	-- return the frame's special container.
	function advF:GetContainerList(frame)
		local rList = {}
		if frame and frame:IsObjectType(_Widget) then
			for i = 1, frame.TabNum do
				tab = frame:GetTabByIndex(i)
				rList[tab.Container] = frame.Name.."_TabContainer_"..i
			end
		end
		
		return rList
	end
	
	-- return the frame's special build-code.Just return the containers.Others would be generated automatically
	function advF:GetFrame(frame)
		local rList = {}
		
		if frame and frame:IsObjectType(_Widget) then
			for i = 1, frame.TabNum do
				tab = frame:GetTabByIndex(i)
				tinsert(rList, frame.Name.."_TabContainer_"..i.." = "..frame.Name..":AddTab(\""..tab.Text.."\").Container")
			end
		end
		
		return table.concat(rList, "\n")
	end

	-- Register
	IGAS.Studio:RegisterAdvanceFrame(_Widget, advF)
end